//
//  WEXOriginalStatusView.swift
//  microBlog
//
//  Created by apple on 16/4/20.
//  Copyright © 2016年 Hill. All rights reserved.
//

import UIKit
import SnapKit
import YYText

class WEXOriginalStatusView: UIView {
    
    // 设置view的底部边界
    var bottomCons: Constraint?
    
    // MARK: - 获取用户信息
    var statusViewModel: WEXStatusViewModel? {
        
        didSet{
            
            // 设置头像
            igv_headIcon.sd_setImageWithURL(NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_small"))
            // 设置昵称
            lb_userName.text = statusViewModel?.status?.user?.name
            // 设置会员图标
            igv_memberIcon.image = statusViewModel?.memberImage
            // TODO : 时间 (会重新设置)
//            lb_timeLabel.text = statusViewModel?.status?.created_at
            lb_timeLabel.text = statusViewModel?.creatAtText
            
            // 设置来源
            lb_sourceLabel.text = statusViewModel?.sourceText
            // 设置正文内容
//            lb_content.text = statusViewModel?.status?.text
            lb_content.attributedText = statusViewModel?.originalAttributedString
            
            bottomCons?.uninstall()
            
            if let pic_urls = statusViewModel?.status?.pic_urls where pic_urls.count > 0 {
                
                
                igv_picture.dataInfo = (pic_urls,statusViewModel!.originalPicSize)
                
//                赋值
//                igv_picture.pic_urls = pic_urls
                
//                显示配图
                igv_picture.hidden = false
                
//                更新配图
                igv_picture.snp_updateConstraints(closure: { (make) -> Void in
                    
                    make.size.equalTo(statusViewModel?.originalPicSize ?? CGSizeZero)
                })
                
//                更新视图底部的约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                  bottomCons = make.bottom.equalTo(igv_picture.snp_bottom).offset(commonMargin).constraint
                })
                
                
            }else{
                
                
//                更新视图底部约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                   bottomCons = make.bottom.equalTo(lb_content).offset(commonMargin).constraint
                })
                
//                隐藏配图
                igv_picture.hidden = true
                
                
            }
            
            
            
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        
        // 设置原创微博的颜色
        backgroundColor = UIColor.whiteColor()
        
        
        // 添加子控件
        addSubview(igv_headIcon)
        addSubview(lb_userName)
        addSubview(igv_memberIcon)
        addSubview(lb_timeLabel)
        addSubview(lb_sourceLabel)
        addSubview(igv_verified)
        addSubview(lb_content)
        addSubview(igv_picture)
        
        
        //设置用户头衔约束
        igv_headIcon.snp_makeConstraints { (make) -> Void in
            
            make.leading.top.equalTo(self).offset(commonMargin)
            make.width.height.equalTo(35)
            
        }
        
        //设置用户名约束
        lb_userName.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(igv_headIcon)
            make.leading.equalTo(igv_headIcon.snp_trailing).offset(commonMargin)
            
        }
        
        //设置会员的约束
        igv_memberIcon.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(lb_userName)
            make.leading.equalTo(lb_userName.snp_trailing).offset(commonMargin)
        }
        
        //设置时间的约束
        lb_timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(igv_headIcon)
            make.leading.equalTo(igv_headIcon.snp_trailing).offset(commonMargin)
        }
        
        //设置来源标签的约束
        lb_sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(lb_timeLabel)
            make.leading.equalTo(lb_timeLabel.snp_trailing).offset(commonMargin)
        }
        
        //设置大v认证的约束
        igv_verified.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(igv_headIcon.snp_bottom).offset(-5)
            make.centerY.equalTo(igv_headIcon.snp_trailing)
            
        }
        
        //设置正文的约束
        lb_content.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(igv_headIcon.snp_bottom).offset(commonMargin)
            make.leading.equalTo(igv_headIcon)
        }
        
        //设置配图的约束
        igv_picture.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(lb_content)
            make.top.equalTo(lb_content.snp_bottom).offset(commonMargin)
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
           bottomCons = make.bottom.equalTo(lb_content.snp_bottom).offset(commonMargin).constraint
        }
        
    }
    
    // MARK: - 懒加载控件
    // 用户头像
    private lazy var igv_headIcon: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_small"))
        
        return imageView
    }()
    
    // 头像昵称
    private lazy var lb_userName: UILabel = {
        
        let name = UILabel(textColor: UIColor.darkGrayColor(), textFont: userNameFont)
        name.text = "哈哈"
        
        return name
    }()
    
    // 会员图标
    private lazy var igv_memberIcon: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    // 时间标签
    private lazy var lb_timeLabel: UILabel = {
       
        let time = UILabel(textColor: UIColor.brownColor(), textFont: sendTimeFont)
        time.text = "刚刚"
        
        return time
        
    }()
    
    // 来源标签
    private lazy var lb_sourceLabel: UILabel = {
       let source = UILabel(textColor: UIColor.blueColor(), textFont: sourceFont)
        
        source.text = "来自 天地会上海分舵"
        
        return source
        
    }()
    
    // 微博认真图标
    private lazy var igv_verified: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    
    // 正文内容
    private lazy var lb_content: YYLabel = {
//        
//        let content = UILabel(textColor: UIColor.darkGrayColor(), textFont: contentFont, preferredMaxLayoutWidth: SCREENWIDTH - 2 * commonMargin)
//        
//        
//        return content
        
        let label = YYLabel()
        
        label.userInteractionEnabled = true
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(contentFont)
        label.preferredMaxLayoutWidth = SCREENWIDTH - 2 * commonMargin
        label.numberOfLines = 0
        
        
        return label
        
    }()
    
    
    // 配图内容
    private lazy var igv_picture: WEXPictureView = WEXPictureView.init(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
    
}
